use std::sync::Arc;
use tokio::io::{ReadHalf, WriteHalf};
use tokio::net::TcpStream;
use tokio::sync::Mutex;

/// TCP连接split trait
/// 提供统一的接口来访问split后的读写半部分
pub trait SplitTcp {
    /// 获取读半部分
    fn read_half(&self) -> Arc<Mutex<ReadHalf<TcpStream>>>;
    
    /// 获取写半部分
    fn write_half(&self) -> Arc<Mutex<WriteHalf<TcpStream>>>;
}

/// 创建split TCP连接的辅助函数
/// 将TcpStream分割为读写两半，每个都包装在Arc<Mutex<>>中
pub fn split_tcp_stream(stream: TcpStream) -> (
    Arc<Mutex<ReadHalf<TcpStream>>>,
    Arc<Mutex<WriteHalf<TcpStream>>>,
) {
    let (read_half, write_half) = tokio::io::split(stream);
    (
        Arc::new(Mutex::new(read_half)),
        Arc::new(Mutex::new(write_half)),
    )
}
